package com.wuxin.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.wuxin.ssm.entity.Users;
import com.wuxin.ssm.service.UsersService;
import com.wuxin.ssm.vo.OrderdetailVo;
import com.wuxin.ssm.vo.UsersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @program: untitled
 * @ClassName UsersController
 * @description:
 * @author: 李尧坤
 * @create: 2024−12-10 19:29
 * @Version 1.0
 **/

@Controller
@RequestMapping("/user")
public class UsersController {

    @Autowired
    private UsersService usersService;

    ///user/login.action

    /**
     * 前台的登录功能
     * @param uname 用户名
     * @param upass 密码
     * @param yzm 验证码
     * @param session  存储用户的session
     * @return
     */
    @RequestMapping("/login.action")
    public String login(String uname, String upass, String ustatus,String yzm, HttpSession session){

        //获取到你存的验证码
        String random= (String) session.getAttribute("random");
        //判断输入情况
        if (uname.equals("") || upass.equals("")  || yzm.equals("") ){
            session.setAttribute("msg","信息输入不完整!");
            return "redirect:/login.jsp";
        }
        //判断昵称位数：2-7
        if(uname.length() < 2 || uname.length() > 7){
            session.setAttribute("msg","昵称位数应大于2位，小于7位");
            return "redirect:/login.jsp";
        }
        //判断密码位数：6-18
        if(upass.length() < 6 || upass.length() > 18){
            session.setAttribute("msg","位数应大于6位，小于18位");
            return "redirect:/login.jsp";
        }
        //比对
        if(!yzm.equals(random)){
            //不一致的
            session.setAttribute("msg","验证码输入错误!");
            return "redirect:/login.jsp";
        }
//        //状态
//        if(!ustatus.equals(1)){
//            //不一致的
//            session.setAttribute("msg","用户已封禁!");
//            return "redirect:/login.jsp";
//        }

        //验证码是正确的
        Map<String, Object> map= usersService.login(uname,upass);

        if("ok".equals(map.get("s"))){
            //登录成功了
            session.setAttribute("user",map.get("user"));
            return "redirect:/main.jsp";

        }else{

            session.setAttribute("msg","登录失败!");
            return "redirect:/login.jsp";
        }


        // return "";
    }

    // <form action="${pageContext.request.contextPath}/user/register.action" method="post">
    // 	xmlRequest.send("type=ajaxregister&uname="+uname+"&upass="+upass+"&upass2="+upass2+"&yzm="+yzm);
    @RequestMapping("/register.action")
    public String register(String uname, String upass,String upass2, String yzm,HttpSession session){
        //获取到你存的验证码
        String random= (String) session.getAttribute("random");
        //判断输入情况
        if (uname.equals("") || upass.equals("")  || upass2.equals("") || yzm.equals("") ){
            session.setAttribute("msg","信息输入不完整!");
            return "redirect:/register.jsp";
        }
        //判断昵称位数：2-7
        if(uname.length() < 2 || uname.length() > 7){
            session.setAttribute("msg","昵称位数应大于2位，小于7位");
            return "redirect:/register.jsp";
        }
        //判断密码位数：6-18
        if(upass.length() < 6 || upass.length() > 18){
            session.setAttribute("msg","位数应大于6位，小于18位");
            return "redirect:/register.jsp";
        }
        //比对
        if(!yzm.equals(random)){
            //不一致的
            session.setAttribute("msg","验证码输入错误!");
            return "redirect:/register.jsp";
        }
        //验证码是正确的
        //验证uname是否已被注册
        int num  = usersService.findByUname(uname);
        if (num != 0 ) {
            session.setAttribute("msg","该昵称已被注册!");
            return "redirect:/register.jsp";
        }
        //uname未被注册
        //验证两次密码是否一致
        if (!upass.equals(upass2)) {
            session.setAttribute("msg","两次密码输入不同!");
            return "redirect:/register.jsp";
        }
        //两次密码一致
        //创建成功
        Users user = new Users();
        user.setUname(uname);
        user.setUpass(upass);
        usersService.addUser(user);
        return "redirect:/login.jsp";
    }


    //每页显示5条属性
    public  static  final int PAGE_SIZE=5;

    /**
     *
     * @param page
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/split.action")
    public String split(Integer page, Users users, Model model, HttpSession session){
        //声明一个p代表页码
        Integer p = 1;
        if (page != null) {
            p = page;
        }
        if (page == null) {
            page = p;
        }
        //开始分页
        PageInfo info= usersService.splitPage(page,PAGE_SIZE);
        //存储一下
        model.addAttribute("us",info);

        return "user";
    }


    @RequestMapping("/mySplict.action")
    public String mySplict(Integer page, UsersVo usersVo, Model model, HttpSession session){

        //声明一个p代表页码
        Integer p = 1;

        if (page != null) {
            p=page;
        }
        if (page == null) {
            page = p;
        }

        //多条件组合查询
        PageInfo chaxun = usersService.chaxun(page,PAGE_SIZE,usersVo);

        System.out.println("-------------------------------");
        //存储一下
        model.addAttribute("us",chaxun);

        session.setAttribute("usersVo",usersVo);


        // /admin/product.jsp

        return "user"; //逻辑视图名  redirect:/
    }
}


